package gold.digger;

import gold.vo.ListNode;

/**
 * Created by fanzhenyu02 on 2020/3/1.
 */
public class LC24 {


    public ListNode swapPairs(ListNode head) {
        if (null == head || null == head.next) {
            return head;
        }

        ListNode newHead = head.next;

        ListNode pre = head;
        ListNode af = head.next;
        ListNode third = head.next.next;

        while (pre != null && af != null) {
            af.next = pre;
            pre.next = third == null ? null : (third.next == null ? third : third.next);

            if (third == null) {
                break;
            }
            pre = third;
            af = third.next;
            if (third.next == null) {
                third = null;
            } else {
                third = third.next.next;
            }
        }

        return newHead;
    }

    public void testMethod() {
        LC24 an = new LC24();
        ListNode h1 = new ListNode(1);
        h1.next = new ListNode(2);
        h1.next.next = new ListNode(3);
        h1.next.next.next = new ListNode(4);
        h1.next.next.next.next = new ListNode(5);
        h1.next.next.next.next.next = new ListNode(6);

        System.out.println(an.swapPairs(h1));
    }

    public static void main(String[] args) {
        LC24 an = new LC24();
        an.testMethod();
    }
}
